
package com.yesep.learn.mqtt.server.broker.handler.protocol;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.yesep.learn.mqtt.protocol.IPubRel;
import com.yesep.learn.mqtt.server.common.Constants;
import com.yesep.learn.mqtt.util.ChannelUtil;

import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttFixedHeader;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageFactory;
import io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader;
import io.netty.handler.codec.mqtt.MqttMessageType;
import io.netty.handler.codec.mqtt.MqttQoS;
import io.netty.util.AttributeKey;

/**
   * 消息编号：6
 * @Description: PUBREL连接处理，发布释放，保证传递2
 */
@Service
public class PubRel implements IPubRel {

	private static final Logger LOGGER = LoggerFactory.getLogger(PubRel.class);

	public void process(Channel channel, MqttMessage msg) {
		MqttMessageIdVariableHeader variableHeader = (MqttMessageIdVariableHeader) msg.variableHeader();
		int messageId = variableHeader.messageId();
		String clientId = ChannelUtil.getClientId(channel);
		MqttMessage pubCompMessage = MqttMessageFactory.newMessage(new MqttFixedHeader(MqttMessageType.PUBCOMP, false, MqttQoS.AT_MOST_ONCE, false, 0), MqttMessageIdVariableHeader.from(messageId), null);
		LOGGER.debug("PUBREL - clientId: {}, messageId: {}", clientId, messageId);
		channel.writeAndFlush(pubCompMessage);
	}

}
